document.querySelector('.header .user ul li:nth-child(2)').addEventListener('click', () => {
  document.querySelector('.userInfoChange').classList.add('uactive')
})
document.querySelector('.userInfoChange').addEventListener('click', function (e) {
  if (e.target == this) {
    this.classList.remove('uactive')
  }
})

document.querySelector('.userInfoChange .uinfo_top .top_rghit').addEventListener('click', function () {
  console.log(this)
  this.parentNode.parentNode.parentNode.classList.remove('uactive')

})

document.querySelectorAll('.toSpan span').forEach((item, i) => {

  item.addEventListener('click', function () {
    if (document.querySelector('.uspanActive')) document.querySelector('.uspanActive').classList.remove('uspanActive')
    this.classList.add('uspanActive')
    if (i == 0) {
      document.querySelector('.toLine').style.left = '10px'
      document.querySelector('.infomm').style.display = 'flex'
      document.querySelector('.infoAvater').style.display = 'none'
    } else {
      document.querySelector('.toLine').style.left = '105px'
      document.querySelector('.infoAvater').style.display = 'flex'
      document.querySelector('.infomm').style.display = 'none'
    }
  })
})

//修改时显示头像
document.querySelector('.infoAvater #avatar1').addEventListener('change', function () {
  let file = this.files[0] // 获取选中的文件  
  console.log(file)
  let reader1 = new FileReader() // 创建FileReader对象  
  reader1.onload = function (e) {
    console.log(e.target.result)
    document.querySelector('.infoAvater .avatar img').src = e.target.result
  };
  reader1.readAsDataURL(file) // 将文件读取为Data URL
})

document.querySelector('.dzErr i').addEventListener('click', function () {
  this.parentNode.style.transform = 'translateY(-100px)'
})

document.querySelector('.infoButton button').addEventListener('click', () => {
  let span1 = document.querySelector('.toSpan span:nth-child(1)')

  if (span1.classList.contains("uspanActive")) {
    let oldmm = document.querySelector('.infomm .oldmm')
    let newmm = document.querySelector('.infomm .newmm')
    let qrmm = document.querySelector('.infomm .qrmm')

    if (oldmm.value.trim() == '') return errMsg('当前密码不能为空！')
    if (newmm.value.trim() == '') return errMsg('新密码不能为空！')
    if (newmm.value.trim() == '') return errMsg('确认密码不能为空！')

    $.ajax({
      url: '../control/usermm.php',
      type: 'POST',
      data: { oldmm: oldmm.value.trim() },
      success: (res) => {
        if (res.code == 200) {
          if (newmm.value.trim() === qrmm.value.trim()) {
            let passwrod = qrmm.value.trim()
            $.ajax({
              url: '../control/changeInfo.php',
              type: 'POST',
              data: { pwd: passwrod },
              success: (res) => {
                if (res.code == 200) {
                  saveMsg(res.msg)
                  console.log(res.msg)
                } else {
                  console.log(res.msg)
                  errMsg(res.msg)
                }
              }, error: (err) => {
                console.log(err)
              }
            })
          } else {
            // console.log(123)
            errMsg('两次密码不同')
          }
        } else {
          errMsg(res.msg)
        }
      }, error: (err) => {
        console.log(err)
      }
    })



  } else {
    let avatarFile = document.querySelector('.infoAvater #avatar1').files[0]
    if (!avatarFile) return errMsg('请选择头像图片')

    // 检查文件类型
    if (!avatarFile.type.match('image.*')) {
      return errMsg('请上传图片文件')
    }

    let formData = new FormData()
    formData.append('avatar', avatarFile)

    $.ajax({
      url: '../control/changeInfo.php',
      type: 'POST',
      data: formData,
      contentType: false,
      processData: false,
      success: (res) => {
        if (res.code == 200) {
          saveMsg(res.msg)
          if (res.avatarUrl) {
            document.querySelector('.infoAvater .avatar img').src = '../image/avatar/' + res.avatarUrl
            document.querySelector('.header .user-logining img').src = '../image/avatar/' + res.avatarUrl
          }
        } else {
          errMsg(res.msg)
        }
      },
      error: (err) => {
        console.log(err)
        errMsg('上传失败')
      }
    })
  }

})

//错误提示函数
function errMsg(msg) {
  let dzErr = document.querySelector('.dzErr')
  dzErr.querySelector('span').textContent = msg
  dzErr.style.transform = 'translateY(0)'
  setTimeout(() => {
    dzErr.style.transform = 'translateY(-100px)'
  }, 3000)
}
function saveMsg(msg) {
  let saveMsg = document.querySelector('.saveMsg')
  saveMsg.querySelector('span').textContent = msg
  saveMsg.style.transform = 'translateY(0)'
  setTimeout(() => {
    saveMsg.style.transform = 'translateY(-100px)'
  }, 2000)
}


function gourl() {
  let url = window.location.pathname
  window.location.href = url
}